

/**
 * 作者:TC
 * 时间:2023/12/31 20:49
 * 描述:没有bug
 */
import java.util.Scanner;
public class QuickSort {
    // 定义一个Team类
    public static class Team {
        String name;
        int num;
        int punish;

        public Team(String name, int num, int punish) {
            this.name = name;
            this.num = num;
            this.punish = punish;
        }
    }

    public static void main(String[] args) {
        int N;
        String Tname;
        int num, punish;
        System.out.println("请输入队伍的数量");
        Scanner Sc = new Scanner(System.in);
        N = Sc.nextInt();
        // new一个对象数组
        Team[] T = new Team[N];
        for (int i = 0; i < N; i++) {
            System.out.println("请输入队伍的名称,得分，罚时间：");
            Tname = Sc.next();
            num = Sc.nextInt();
            punish = Sc.nextInt();
            T[i] = new Team(Tname, num, punish);
        }

        for (int i = 0; i < T.length; i++) {
            System.out.println(
                    "第" + (i + 1) + "队伍：" + " name:" + T[i].name + " num:" + T[i].num + " punish:" + T[i].punish);
        }

        QuickSort(T, 0, T.length - 1);

        System.out.println("快速排序后的结果为");
        for (int i = 0; i < T.length; i++) {
            System.out.println(
                    "第" + (i + 1) + "名：" + " name:" + T[i].name + " num:" + T[i].num + " punish:" + T[i].punish);
        }
        System.out.println("\n");
    }
//快排算法
    public static void QuickSort(Team[] a, int low, int high) {
        if (low < high) {
            int pivot = Partition(a, low, high);
            QuickSort(a, low, pivot - 1);
            QuickSort(a, pivot + 1, high);
        }
    }
//根据中间位置划分
    public static int Partition(Team[] a, int low, int high) {
        Team pivot = a[high];
        int i = low - 1;
        for (int j = low; j < high; j++) {
            if (CompareTeam(a[j], pivot) <= 0) {
                i++;
                swapnode(a, i, j);
            }
        }
        swapnode(a, i + 1, high);
        return i + 1;
    }
    //构建一个函数返回两个队伍比较后的大小返回得分与乘法时间之差 分开讨论三种情况 两者的得分相同时，惩罚时间是否相同 以及
    //以及按照队伍的名称来进行排序
    public static int CompareTeam(Team a, Team b) {
        if (a.num != b.num) {
            return a.num - b.num;
        } else if (a.punish != b.punish) {
            return a.punish - b.punish;
        } else {
            return a.name.compareTo(b.name);
        }
    }

    public static void swapnode(Team[] T, int i, int j) {
        Team team = T[i];
        T[i] = T[j];
        T[j] = team;
    }
}